package ink.sunflowerk.service.impl;


import ink.sunflowerk.dao.StudentDao;
import ink.sunflowerk.pojo.PagerBean;
import ink.sunflowerk.pojo.Student;
import ink.sunflowerk.service.StudentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImp implements StudentService {

    @Autowired
    private StudentDao studentDao;


    @Override
    public Integer addOne(Student t) {
        return studentDao.insertStudent(t);
    }

    @Override
    public Integer deleteOne(int pk) {
        return studentDao.deleteStudentById(pk);
    }

    @Override
    public int updateOne(Student t) {
        return studentDao.updateStudentById(t);
    }

    @Override
    public Student getOneByKey(int pk) {
        return studentDao.getOneByKey(pk);
    }

    @Override
    public List<Student> getAll() {
        return studentDao.getAll();
    }

    public PagerBean<Student> getPageBean(Integer pageNum) {
        //获取总记录数
        int count = studentDao.getCount();
        //分页条数：
        // 如果是最后一页+1 的话进行判断，
        int test = count % 5 == 0 ? (count % 5) : (count % 5 + 1);
        if (test != 0) {
            pageNum = test >= pageNum ? pageNum : pageNum - 1;
        }

        //test =0 5%5=0 只有第一页
        if (test == 0 && pageNum >= 1) {
            pageNum = 1;
        }
        PagerBean<Student> pagerBean = new PagerBean<>();
        pagerBean.setCurrentPage(pageNum);//当前页
        pagerBean.setRows(5);//分页条数
        pagerBean.setTotalCount(count);//总记录数
        //获取分页结果
        List<Student> list = studentDao.getPageStudent(pagerBean.getStartRow(), pagerBean.getRows());
        pagerBean.setList(list);
        return pagerBean;
    }

}
